package cn.itzzx.lagou.service.impl;

import cn.itzzx.lagou.dao.LagouTokenMapper;
import cn.itzzx.lagou.pojo.LagouToken;
import cn.itzzx.lagou.service.LagouTokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * TODO
 *
 * @author Martin
 * @since 1.0
 */
@Service
public class LagouTokenServiceImpl implements LagouTokenService {

    @Resource
    private LagouTokenMapper lagouTokenMapper;


    @Override
    public boolean register(String email, String password, String code) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(UUID.randomUUID().toString());
        lagouToken.setEmail(email);
        lagouToken.setPassword(password);

        int result = lagouTokenMapper.insert(lagouToken);
        if(result > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean isRegistered(String email) {
        LagouToken lagouToken = lagouTokenMapper.load(email);
        if(lagouToken == null){
            return false;
        }
        return true;
    }

    @Override
    public LagouToken login(String email, String password) {
        Map<String,Object> params = new HashMap<>();
        params.put("email",email);
        params.put("password",password);
        LagouToken lagouToken = lagouTokenMapper.login(params);
        return lagouToken;
    }

    @Override
    public String info(String token) {
        LagouToken lagouToken = lagouTokenMapper.loadByToken(token);
        if(lagouToken == null){
            return "";
        }
        return lagouToken.getEmail();
    }
}
